package com.epmis.epmis.controller;

import com.epmis.epmis.Service.ProjectService;
import com.epmis.epmis.pojo.Project;
import com.epmis.epmis.pojo.User;
import com.epmis.epmis.utils.ImageUitl;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Sets;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Controller
public class ProjectController {
    @Resource
    ProjectService projectService;


    @PostMapping("/getAllProjectTable")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getAllProjectTable(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = (String) map.get("id");
        int enterpriseId = Integer.valueOf((String)map.get("enterpriseId"));
        return projectService.getAllProjectTable(pageCode,pageSize,id,enterpriseId);
    }

    @PostMapping("/getAllProjectList")
    @ResponseBody
    public List<HashMap<String,Object>> getAllProjectList(@RequestBody HashMap<String,Object> map){
        String id = (String) map.get("id");
        int enterpriseId = Integer.valueOf((String)map.get("enterpriseId"));
        return projectService.getProjectList(id,enterpriseId);
    }

    @PostMapping("/getAllTopProjectList")
    @ResponseBody
    public List<HashMap<String,Object>> getAllTopProjectList(@RequestBody HashMap<String,Object> map){
        String id = (String) map.get("id");
        int enterpriseId = Integer.valueOf((String)map.get("enterpriseId"));
        return projectService.getTopProjectList(id,enterpriseId);
    }

    @PostMapping("/getPublicProjectTable")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getPublicProjectTable(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        int enterpriseId = Integer.valueOf((String)map.get("enterpriseId"));;
        String id = (String) map.get("id");
        return projectService.getPublicProject(pageCode,pageSize,id,enterpriseId);
    }

    @PostMapping("/getPrivateProjectTable")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getPrivateProjectTable(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = (String) map.get("id");
        return projectService.getPrivateProject(pageCode,pageSize,id);
    }

    @PostMapping("/findAllProject")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> findAllProject(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = (String) map.get("id");
        String content = (String) map.get("content");
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return projectService.findAllProject(pageCode,pageSize,id,content,enterpriseId);
    }

    @PostMapping("/findAllDeleteProjectTable")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> findAllDeleteProject(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = (String) map.get("id");
        String content = (String) map.get("content");
        int enterpriseId = Integer.valueOf((String)map.get("enterpriseId"));
        return projectService.findAllDeleteProject(pageCode,pageSize,id,content,enterpriseId);
    }

    @PostMapping("/getAllTarget")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getAllTarget(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = (String) map.get("id");
        return projectService.getAllTarget(pageCode,pageSize,id);
    }

    @PostMapping("/getSearchAllTarget")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getSearchAllTarget(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = (String) map.get("id");
        String missionId = (String) map.get("MissionId");
        return projectService.getSearchAllTarget(pageCode,pageSize,id,missionId);
    }

    @PostMapping("/getDangerMission")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getDangerMission(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = (String) map.get("id");
        return projectService.getDangerMission(pageCode,pageSize,id);
    }

    @PostMapping("/getCompleteMission")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getCompleteMission(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = (String) map.get("id");
        return projectService.getCompleteMission(pageCode,pageSize,id);
    }

    @PostMapping("/getAllProjectMission")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getAllProjectMission(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = (String) map.get("id");
        return projectService.getAllProjectMission(pageCode,pageSize,id);
    }

    @PostMapping("/getSearchProjectMission")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getSearchProjectMission(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String MissionId = (String) map.get("missionId");
        String id = (String) map.get("id");
        return projectService.getSearchProjectMission(pageCode,pageSize,id,MissionId);
    }

    @PostMapping("/getMyProjectMission")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getMyProjectMission(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String principal_id = (String) map.get("principal_id");
        String id = (String) map.get("id");
        return projectService.getMyProjectMission(pageCode,pageSize,id,principal_id);
    }

    @PostMapping("/getDelayProjectMission")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getDelayProjectMission(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = (String) map.get("id");
        return projectService.getDelayProjectMission(pageCode,pageSize,id);
    }

    @PostMapping("/getAllCost")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getAllCost(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = (String) map.get("id");
        return projectService.getAllCost(pageCode,pageSize,id);
    }

    @PostMapping("/getSearchCost")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getSearchCost(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String missionId = (String) map.get("missionId");
        String id = (String) map.get("id");
        return projectService.getSearchCost(pageCode,pageSize,id,missionId);
    }

    @PostMapping("/getMyCost")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getMyCost(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String principal_id  = (String) map.get("principal_id");
        String id = (String) map.get("id");
        return projectService.getMyCost(pageCode,pageSize,id,principal_id);
    }

    @PostMapping("/getBackCost")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getBackCost(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = (String) map.get("id");
        return projectService.getBackCost(pageCode,pageSize,id);
    }

    @PostMapping("/getHappenCost")
    @ResponseBody
    public PageInfo<HashMap<String,Object>> getHappenCost(@RequestBody HashMap<String,Object> map){
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = (String) map.get("id");
        return projectService.getHappenCost(pageCode,pageSize,id);
    }

    @PostMapping("updateDeleteProject")
    @ResponseBody
    public int updateDeleteProject (@RequestBody HashMap<String,Object> map){
        return projectService.updateDeleteProject((int)map.get("id"));
    }
//
//    @PostMapping("deleteProject")
//    @ResponseBody
//    public int deleteProject (@RequestBody HashMap<String,Object> map){
//        return projectService.deleteProject((int)map.get("id"));
//    }

    @PostMapping("updateProjectName")
    @ResponseBody
    public int updateProjectName (@RequestBody HashMap<String,Object> map){
        String project_name = (String)map.get("project_name");
        int id = Integer.valueOf((String)map.get("id"));
        return projectService.updateProjectName(project_name,id);
    }

    @PostMapping("cancelTop")
    @ResponseBody
    public int cancelTop(@RequestBody HashMap<String,Object> map){
        int id = Integer.valueOf((String)map.get("id"));
        return projectService.cancelTop(id);
    }

    @PostMapping("ToTop")
    @ResponseBody
    public int ToTop(@RequestBody HashMap<String,Object> map){
        int id = Integer.valueOf((String)map.get("id"));
        return projectService.ToTop(id);
    }

    @PostMapping("updateProjectNumber")
    @ResponseBody
    public int updateProjectNumber (@RequestBody HashMap<String,Object> map){
        String project_number = (String)map.get("project_number");
        int id = Integer.valueOf((String)map.get("id"));
        String enterprise_id = (String)map.get("enterprise_id");
        return projectService.updateProjectNumber(project_number,id,enterprise_id);
    }

    @PostMapping("updateProjectRouge")
    @ResponseBody
    public int updateProjectRouge (@RequestBody HashMap<String,Object> map){
        int rouge = (int) map.get("rouge");
        int id = Integer.valueOf((String)map.get("id"));
        return projectService.updateProjectRouge(rouge,id);
    }

    @PostMapping("updateProjectDescription")
    @ResponseBody
    public int updateProjectDescription (@RequestBody HashMap<String,Object> map){
        String description = (String)map.get("description");
        int id = Integer.valueOf((String)map.get("id"));
        return projectService.updateProjectDescription(description,id);
    }

    @PostMapping("updateProjectState")
    @ResponseBody
    public int updateProjectState (@RequestBody HashMap<String,Object> map){
        String state = (String)map.get("state");
        int id = Integer.valueOf((String)map.get("id"));
        return projectService.updateProjectState(state,id);
    }

    @PostMapping("updateProjectStateDescription")
    @ResponseBody
    public int updateProjectStateDescription (@RequestBody HashMap<String,Object> map){
        String state_description = (String)map.get("state_description");
        int id = Integer.valueOf((String)map.get("id"));
        return projectService.updateProjectStateDescription(state_description,id);
    }

    @PostMapping("updateProjectCustomerUnit")
    @ResponseBody
    public int updateProjectCustomerUnit (@RequestBody HashMap<String,Object> map){
        String customer_unit = (String)map.get("customer_unit");
        int id = Integer.valueOf((String)map.get("id"));
        return projectService.updateProjectCustomerUnit(customer_unit,id);
    }

    @PostMapping("updateProjectCustomerRepresentative")
    @ResponseBody
    public int updateProjectCustomerRepresentative (@RequestBody HashMap<String,Object> map){
        String customer_representative = (String)map.get("customer_representative");
        int id = Integer.valueOf((String)map.get("id"));
        return projectService.updateProjectCustomerRepresentative(customer_representative,id);
    }

    @PostMapping("updateProjectType")
    @ResponseBody
    public int updateProjectType (@RequestBody HashMap<String,Object> map){
        String project_type = (String)map.get("project_type");
        int id = Integer.valueOf((String)map.get("id"));
        return projectService.updateProjectType(project_type,id);
    }

    @PostMapping("updateStartDate")
    @ResponseBody
    public int updateStartDate (@RequestBody HashMap<String,Object> map) throws ParseException {
        SimpleDateFormat date =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start_date= date.parse((String)map.get("start_date"));
        int id = Integer.valueOf((String)map.get("id"));
        return projectService.updateStartDate(start_date,id);
    }

    @PostMapping("updateOverDate")
    @ResponseBody
    public int updateOverDate (@RequestBody HashMap<String,Object> map) throws ParseException {
        SimpleDateFormat date =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date over_date= date.parse((String)map.get("over_date"));
        int id = Integer.valueOf((String)map.get("id"));
        return projectService.updateOverDate(over_date,id);
    }

    @PostMapping("updateProjectPrincipal")
    @ResponseBody
    public int updateProjectPrincipal (@RequestBody HashMap<String,Object> map){
        int principal_id = (int) map.get("principal_id");
        int id = Integer.valueOf((String)map.get("id"));
        return projectService.updateProjectPrincipal(principal_id,id);
    }


    @PostMapping("getProjectMessage")
    @ResponseBody
    public HashMap<String, Object> getProjectMessage(@RequestBody HashMap<String,Object> map){
        return projectService.getProjectMessage(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("updateProjectMembers")
    @ResponseBody
    public int updateProjectMembers(@RequestBody HashMap<String,Object> map){
        ArrayList oldMembers = (ArrayList) map.get("oldMembers");
        ArrayList newMembers = (ArrayList) map.get("newMembers");
        int projectId = Integer.valueOf((String)map.get("id"));
        return projectService.updateProjectMembers(oldMembers,newMembers,projectId);
    }

    @PostMapping("deleteProject1")
    @ResponseBody
    public int deleteProject1(@RequestBody HashMap<String,Object> map){
        int deleteId = Integer.valueOf((String)map.get("deleteId"));
        int projectId = Integer.valueOf((String)map.get("id"));
        return projectService.deleteProject1(deleteId,projectId);
    }



}
